package com.zw.yzk.kabu.presenter;

import android.support.v7.app.AppCompatActivity;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.data.network.entity.TickerOrderDetailsEntity;
import com.zw.yzk.kabu.data.network.task.TicketOrderDetailsTask;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.mapper.TicketOrderDetailsMapper;
import com.zw.yzk.kabu.view.view.TicketOrderDetailsView;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by zhanwei on 2017/3/9.
 */

public class TicketOrderDetailsPresenter implements Presenter {

    private UseCase getTicketOrderDetails;
    private AppCompatActivity activity;
    private TicketOrderDetailsMapper mapper;
    private TicketOrderDetailsView detailsView;


    @Inject
    public TicketOrderDetailsPresenter(@Named("ticketOrderUseCase") UseCase getTicketOrderDetails,
                                       AppCompatActivity activity,
                                       TicketOrderDetailsMapper mapper) {
        this.getTicketOrderDetails = getTicketOrderDetails;
        this.activity = activity;
        this.mapper = mapper;
    }

    public void setView(TicketOrderDetailsView view) {
        this.detailsView = view;
    }

    public void getTicketDetails(String orderId) {
        getTicketOrderDetails.execute(new GetTicketDetailsSubscriber(activity), new TicketOrderDetailsTask(orderId));
    }

    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        getTicketOrderDetails.unSubscribe();
    }

    private class GetTicketDetailsSubscriber extends BaseSubscriber<TickerOrderDetailsEntity> {

        public GetTicketDetailsSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(TickerOrderDetailsEntity entity) {
            super.onNext(entity);
            detailsView.getTicketOrderDetailsSuccess(mapper.transfer(entity));
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.get_ticket_order_details_failed);
        }
    }
}
